package xyz.labmem.main.modular.app.gmjj.controller;

import com.alibaba.excel.EasyExcel
import xyz.labmem.main.modular.common.service.selectFormPage
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import xyz.labmem.base.core.annotion.BusinessLog
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.main.modular.common.pojo.FormPage
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.common.request.ComDelBatchReq
import xyz.labmem.base.core.common.response.ComPageAndColumnRes
import xyz.labmem.base.core.common.response.ResData
import xyz.labmem.base.core.enums.LogAnnotationOpTypeEnum
import xyz.labmem.base.core.result.RP
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.sys.base.BaseController
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.main.modular.app.gmjj.entity.sub.YgcjyccjbSub
import xyz.labmem.main.modular.app.gmjj.mapper.YgcjyccjbSub_Mapper
import xyz.labmem.main.modular.common.service.appDownloadTemplate
import javax.servlet.http.HttpServletResponse

/**
 * 油罐车（加油车）采集_子Controller
 * 
 * @author ruoyi
 * @date 2022-03-08
 */

@RestController
@Api(tags = ["油罐车（加油车）采集_子"])
@RequestMapping("${PathPrefixConst.APP_PATH_PREFIX}jtjygcjyccjbsub")
class YgcjyccjbSub_Controller (
    private var ygcjyccjbSubMapper: YgcjyccjbSub_Mapper
) : BaseController() {

    @ApiOperation(value = "获取详情")
    @GetMapping(value = ["/{id}"])
    fun getInfo(@PathVariable("id") id: String): RV<*> {
        return success(ResData<YgcjyccjbSub>().apply {
            data = ygcjyccjbSubMapper.selectById(id)
            list = getFields2(YgcjyccjbSub::class)
        })
    }

    /**
     * 查询油罐车（加油车）采集_子分页展示
     */
    @PostMapping("page")
    @ApiOperation(value = "分页展示油罐车（加油车）采集_子")
    fun page(@RequestBody @Validated(BaseVerify.page::class) req: FormPage): RP<ComPageAndColumnRes<YgcjyccjbSub>> {
        val pages =  ygcjyccjbSubMapper.selectPage(
            req.IPage(),
            selectFormPage<YgcjyccjbSub>(req.query)
        )
        return pageWithColumn(pages, YgcjyccjbSub::class.java)
    }

    @GetMapping("getFields")
    @ApiOperation(value = "获取所有字段油罐车（加油车）采集_子")
    fun getFields(): RV<ArrayList<xyz.labmem.base.core.common.response.PageColumn>> {
        return success(getFields(YgcjyccjbSub::class))
    }
    @BusinessLog(title = "修改数据油罐车（加油车）采集_子", opType = LogAnnotationOpTypeEnum.UPDATE)
    @PostMapping("update")
    @ApiOperation(value = "修改数据油罐车（加油车）采集_子")
    fun update(@RequestBody req: YgcjyccjbSub): RV<*> {
        return if (req.id.isNotBlank()) {
            resultJudgment(req.updateById())
        } else
            failedMsg("id不能为空")
    }
    @BusinessLog(title = "批量删除油罐车（加油车）采集_子", opType = LogAnnotationOpTypeEnum.DELETE)
    @DeleteMapping("delBatch")
    @ApiOperation(value = "批量删除油罐车（加油车）采集_子")
    fun delBatch(@RequestBody @Validated req: ComDelBatchReq): RV<*> {
        return resultJudgment(ygcjyccjbSubMapper.deleteBatchIds(req.ids), "删除成功！", "没有数据被删除")
    }

    @BusinessLog(title = "导入油罐车（加油车）采集_子", opType = LogAnnotationOpTypeEnum.IMPORT)
    @PostMapping("importData")
    @ApiOperation(value = "导入数据,“excel格式按照模板填写!”")
    fun importData(@RequestPart("file") file: MultipartFile,pkid : String): RV<*> {
        var list = EasyExcel.read(file.getInputStream())
            .head(YgcjyccjbSub::class.java).sheet().doReadSync<YgcjyccjbSub>()
        list.forEach{
            it.pkId = pkid;
        }
        ygcjyccjbSubMapper.insertBatchSomeColumn(list)
        return success()
    }

    @GetMapping("downloadTemplate")
    @ApiOperation(value = "下载导入模板")
    fun downloadTemplate(response: HttpServletResponse) {
        appDownloadTemplate(this, response, YgcjyccjbSub(),ygcjyccjbSubMapper)
    }
}
